//
//  SearchResultWithProductViewController.m
//  ChunLaiZhuangClient
//
//  Created by zouzhisheng on 16/3/13.
//  Copyright © 2016年 CLZ. All rights reserved.
//

#import "SearchResultWithProductViewController.h"
#import "CustomProductTableViewCell.h"
#import "ProductListsTableViewCell.h"
#import "SearchResultModel.h"
@interface SearchResultWithProductViewController ()<UITableViewDataSource,UITableViewDelegate>{
    UILabel *placeHoldLabel;
    SearchResultModel *resultModel;  //产品数据
    NSMutableArray *productsArray;  //数据源
}
@property(nonatomic,assign)int pageNum;
@property(nonatomic,copy)NSString *order; //升降序
@end

@implementation SearchResultWithProductViewController

- (void)viewDidLoad {
    self.pageNum=1;
    self.order=@"DESC";
    productsArray=[[NSMutableArray alloc] init];

    [super viewDidLoad];
    [self.view addSubview:self.searchBarView];
    [self loadData];
    // Do any additional setup after loading the view.
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:YES];

}
-(void)loadData{
     __weak typeof(self) weakSelf = self;
     [[LKDHud shareHud] showLoadingView];
    [ApiClient getSearchResultKeyword:self.placeHoldStr Sort:@"price" order:self.order page:[NSString stringWithFormat:@"%d",self.pageNum] DidFinishLoaded:^(SearchResultModel *model, NSString *path) {
         [[LKDHud shareHud] hideLoadingView];
        resultModel=model;
        if (weakSelf.pageNum==1) {
            [productsArray removeAllObjects];
        }
        if (model.data.total_page.integerValue<=1) {
            weakSelf.displayView.mj_footer.hidden=YES;
        }else{
            weakSelf.displayView.mj_footer.hidden=NO;
        }
        [productsArray addObjectsFromArray:resultModel.data.products];
        resultModel.data.products=productsArray;
        [[LKDHud shareHud] hideLoadingView];
        if (![weakSelf.view.subviews containsObject:weakSelf.displayView]) {
            [weakSelf.view addSubview:weakSelf.displayView];
        }else{
            [weakSelf.displayView reloadData];
        }
        //结束下拉刷新和加载更多状态
        [weakSelf.displayView.mj_header endRefreshing];
        if (model.data.total_page.integerValue == self.pageNum) {
            [weakSelf.displayView.mj_footer endRefreshingWithNoMoreData];
        } else {
            [weakSelf.displayView.mj_footer endRefreshing];
        }

    } didFailLoaded:^(NSError *error, NSString *path) {
         [[LKDHud shareHud] hideLoadingView];
    }];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(UIView*)searchBarView{
    if (!_searchBarView) {
        _searchBarView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 128/2)];
        _searchBarView.backgroundColor=[UIColor colorWithHexString:@"fafafa"];
        UIButton *backBt=[UIButton buttonWithType:UIButtonTypeCustom];
        backBt.frame=CGRectMake(0, 25,45, 35);
        [backBt setImage:[UIImage imageNamed:@"nav_back"] forState:UIControlStateNormal];
        [backBt addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
        [_searchBarView addSubview:backBt];
        
        UIButton *searchBt=[UIButton buttonWithType:UIButtonTypeCustom];
        searchBt.frame=CGRectMake(45, 25, SCREEN_WIDTH-45-80, 34);
        searchBt.layer.cornerRadius=18;
        searchBt.clipsToBounds=YES;
        searchBt.layer.borderColor=[UIColor colorWithHexString:@"d8d8f8"].CGColor;
        searchBt.layer.borderWidth=0.5;
        searchBt.backgroundColor=[UIColor whiteColor];
        [searchBt addTarget:self action:@selector(searchAction:) forControlEvents:UIControlEventTouchUpInside];
        UIImageView *searchIcon=[[UIImageView alloc] initWithFrame:CGRectMake(20, 9, 16, 16)];
        searchIcon.image=[UIImage imageNamed:@"icon_search"];
        
        [searchBt addSubview:searchIcon];
        placeHoldLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 2, 200, 30)];
        placeHoldLabel.textColor=[UIColor colorWithHexString:@"c8c8c8"];
        placeHoldLabel.font=[UIFont systemFontOfSize:12];
        placeHoldLabel.left=searchIcon.right+7;
        placeHoldLabel.text=self.placeHoldStr;
        [searchBt addSubview:placeHoldLabel];
        UIButton *cancelBt=[UIButton buttonWithType:UIButtonTypeCustom];
        cancelBt.frame=CGRectMake(searchBt.right,25, 64, 35);
        [cancelBt setTitle:@"价格" forState:UIControlStateNormal];
        [cancelBt setImage:[UIImage imageNamed:@"icon_price_low"] forState:UIControlStateNormal];
        [cancelBt setImage:[UIImage imageNamed:@"icon_price_high"] forState:UIControlStateSelected];
        [cancelBt setImageEdgeInsets:UIEdgeInsetsMake(0, 55, 0, 0)];
        cancelBt.titleLabel.font=[UIFont systemFontOfSize:15];
        [cancelBt setTitleColor:[UIColor colorWithHexString:@"000000"] forState:UIControlStateNormal];
        [cancelBt addTarget:self action:@selector(priceAction:) forControlEvents:UIControlEventTouchUpInside];
        [_searchBarView addSubview:cancelBt];
        [_searchBarView addSubview:searchBt];
    }
    return _searchBarView;
}

-(UITableView*)displayView{
    __weak typeof(self) weakSelf = self;

    if (!_displayView) {
        _displayView=[[UITableView alloc] initWithFrame:CGRectMake(0,self.searchBarView.bottom, SCREEN_WIDTH, SCREEN_HEIGHT-64)];
         _displayView.delegate=self;
        _displayView.dataSource=self;
        _displayView.backgroundColor=[UIColor clearColor];
        _displayView.separatorStyle=UITableViewCellSeparatorStyleNone;
        _displayView.tableFooterView=[[UIView alloc] initWithFrame:CGRectZero];
        UIScrollView *scrollow=(UIScrollView*)_displayView;
        scrollow.keyboardDismissMode=UIScrollViewKeyboardDismissModeOnDrag;
        _displayView.mj_header = [CLZRefreshHeader headerWithRefreshingBlock:^{
            [weakSelf loadData];
        }];
        
        _displayView.mj_footer = [CLZRefreshFooter footerWithRefreshingBlock:^{
            self.pageNum++;
            [weakSelf loadData];
        }];

    }
    return _displayView;
}
#pragma mark-UITableViewDataSource,UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (resultModel.data.products.count>0) {
        ProductListsTableViewCell *cell=[ProductListsTableViewCell cellWithTableView:tableView];
        [cell setSearchModel:resultModel];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        return cell;
 
    }else{
        QLKEmptyCellTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:kEmptyCellTableViewCellIdentify];
        if (!cell) {
            cell = [[QLKEmptyCellTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kEmptyCellTableViewCellIdentify];
        }
        [cell setLogoImage: @"fw_icon_no_content" cellHeight:self.displayView.height];
        cell.titleLabel.text = @"暂无相关内容" ;
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        return cell;

    }
  

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (resultModel.data.products.count>0) {
        int count=resultModel.data.products.count;
        int lineNum=0;
        if (count%2==0) {
            lineNum=count/2;
        }else{
            lineNum=count/2+1;
        }
        
        return lineNum*240;
    }else{
        return tableView.height;
    }
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
}

-(void)backAction:(UIButton*)bt{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)priceAction:(UIButton*)bt{
    bt.selected=!bt.selected;
    if (bt.selected) {
        self.order=@"ASC";
    }else{
        self.order=@"DESC";
    }
    self.pageNum=1;
    [self loadData];
}

-(void)searchAction:(UIButton*)bt{
    [self.navigationController popViewControllerAnimated:NO];
    
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
